﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;

public class UIshower : Singleton<UIshower>
{
    Dictionary<Nodeal, Tweener> doTween = new Dictionary<Nodeal, Tweener>();
    public override void FixedAwake(){}

    #region 修改位置和大小的延迟处理
        /// <summary>
        /// 将shower显示出来
        /// isUI是是否显示的是UI
        /// TransForm是显示的物体
        /// Vector是显示的位置
        /// useTime是调用函数开始显示所用时间
        /// dueTime是显示多久后自动处理shower
        /// 
        /// </summary>
        public void ShowFromPosition(bool isUI, bool isdue, Transform trans, Vector3 vector3, float useTime, float dueTime=0, string str = "")
        {

            Tweener tw;
            if (trans != null)
            {
                if (isUI)
                {
                    tw = trans.GetComponent<RectTransform>().DOLocalMove(vector3, useTime);
                }
                else tw = trans.GetComponent<Transform>().DOMove(vector3, useTime).OnComplete(()=> { });
                tw.SetAutoKill(false);
                tw.PlayForward();
                if(isdue)
                StartCoroutine(WaitDeal(trans, dueTime));
                SetText(trans, str);
            }
        }
        /// <summary>
        /// 将shower显示出来
        /// TransForm是显示的物体
        /// Vector是显示的大小
        /// useTime是调用函数开始显示所用时间
        /// dueTime是显示多久后自动处理shower
        /// <param name="trans"></param>
        /// <param name="size"></param>
        /// <param name="useTime"></param>
        /// <param name="dueTime"></param>
        /// <param name="str"></param>
        /// </summary>
        public void ShowFromSize(Transform trans, Vector3 size, float useTime, float dueTime, string str = "")
        {
            Tweener tw;
            if (trans != null)
            {
                tw = trans.DOScale(size, useTime);
                tw.SetAutoKill(false);
                tw.PlayForward();
                StartCoroutine(WaitDeal(trans, dueTime));
                SetText(trans, str);
            }
        }
        /// <summary>
    /// 淡入淡出
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="cycleTimes"></param>
        public void FiAFo(Transform tr, int useTimes, bool isHide)
    {

        tr.GetComponent<Image>().DOFade(0, useTimes).SetUpdate(true).OnComplete(() =>
        {
            if (isHide)
            {
                tr.GetComponent<Image>().DOFade(1, 0.001f).SetUpdate(true);
                tr.gameObject.SetActive(false);
            }
        });
    }//透明
        /// <summary>
         /// 延迟处理
         /// </summary>
         /// <param name="dueTime"></param>
         /// <returns></returns>
        IEnumerator WaitDeal(Transform trans, float dueTime)
            {
                yield return new WaitForSeconds(dueTime);
                trans.DOPlayBackwards();
                SetText(trans, "", true);
            }//延迟处理
    #endregion

    public void SetText(Transform tran, string str,bool isRe=false)
    {
        if (str != ""||isRe)
        {
            if (tran.GetChild(0).GetComponent<TextMesh>() != null)
                tran.GetChild(0).GetComponent<TextMesh>().text = str;
            else tran.GetChild(0).GetComponent<Text>().text = str;
        }
    }//设置字体
    public void StartPlay(Nodeal nodeal)
    {
        Tweener tw;
        doTween.TryGetValue(nodeal,out tw);
        tw.PlayForward();
    }//手动开始
    public void RecPlay(Nodeal nodeal)
    {
        Tweener tw;
        doTween.TryGetValue(nodeal, out tw);
        tw.PlayBackwards();
    }//手动退回


    public void InitDotween(Nodeal nodeal,Tweener tweener)
    {
        tweener.SetAutoKill(false);
        tweener.Pause();
        tweener.SetUpdate(true);
        doTween.Add(nodeal, tweener);
    }
    public void InitHpSlider(Transform tf)
    {
        GameObject go = GameObject.Instantiate(Resources.Load(@"Prefabs\CanvasHp"), new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
        go.transform.SetParent(tf);
        RectTransform rectTransform = go.GetComponent<RectTransform>();
        rectTransform.localPosition = new Vector3(0, 1.5f, -50);
        rectTransform.localScale = new Vector3(0,0, 0);
    }//生成血条
}
public enum Nodeal
{
     StartBg, HelpBg,DeathPanel,SetMusic,Quit
}